package inabit.compiler.lexical.automaton;

import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**Represents a deterministic finite automata*/
/**@author Rafael Rocha*/
public class Automata {
	private String initialState;
	private Set<String> states;
	private List<Transition> transitions;
	private Set<String> finalStates;
	
	public Automata() {
		this.states = new HashSet<String>();
		this.finalStates = new HashSet<String>();
		this.transitions = new ArrayList<Transition>();
	}
	
	/**Sets the initial state of the automata, this state must be contained on
	 * the state set of the automata*/
	public void setInitialState(String s) throws InvalidParameterException{
		if(states.contains(s)) {
			this.initialState = s;
		}else {
			throw new InvalidParameterException("State " + s + " must be contained in automaton");
		}
	}
	
	/**Set the set of states specified by parameter as final states, these states must be contained
	 * on the automata*/
	public void setFinalState(String... states) throws InvalidParameterException{
		for(String state : states) {
			if(this.states.contains(state)) {
				finalStates.add(state);
			}else {
				throw new InvalidParameterException("State " + state + " must be contained in automata");
			}
		}
	}
	
	/**Returns the initial state of the automata*/
	public String getInitialState() {
		return this.initialState;
	}
	
	/**Add a state to the finite automata, there must be no two states with same name*/
	public void addState(String s) throws InvalidParameterException{
		if(this.states.contains(s)) {
			throw new InvalidParameterException("There is already a state with the name " + s + " on the automata");
		}
		this.states.add(s);
	}
	
	/**Add a vector of states to the automata*/
	public void addStates(String... states) throws InvalidParameterException{
		for(String state : states) {
			addState(state);
		}
	}
	
	public void removeState(String s) {
		this.states.remove(s);
		this.finalStates.remove(s);
	}
	
	public boolean isFinalState(String s) {
		return finalStates.contains(s);
	}
	
	/**Creates a transition from sourceState to targetState on automata when the condition is specified by the evalStrategy*/
	public void addTransition(Transition transition) throws InvalidParameterException{
		if(states.contains(transition.getSourceState()) && states.contains(transition.getTargetState())) {
			this.transitions.add(transition);
		}else if(!states.contains(transition.getSourceState())){
			throw new InvalidParameterException("source state " + transition.getSourceState() + " was not found on automata");
		}else {
			throw new InvalidParameterException("target state " + transition.getTargetState() + " was not found on automata");
		}
	}
	
	/**Return the transitions that have state <i>s</i> as source*/
	public Collection<Transition> getTransitionsFrom(String s) {
		Set<Transition> transitionsSet = new HashSet<Transition>();
		for(Transition t : transitions) {
			if(t.getSourceState().equals(s)) {
				transitionsSet.add(t);
			}
		}
		return transitionsSet;
	}
	
	/**Return the transitions that have state <i>s</i> as target*/
	public Collection<Transition> getTransitionsTo(String s) {
		Set<Transition> transitionsSet = new HashSet<Transition>();
		for(Transition t : transitions) {
			if(t.getSourceState().equals(s)) {
				transitionsSet.add(t);
			}
		}
		return transitionsSet;
	}
	
	/**Return the state that have a transition from the state passed by parameter which accept the character c*/
	public String getTargetState(String state, char c) {
		if(states.contains(state)){
			for(Transition t : getTransitionsFrom(state)) {
				if(t.acceptTransition(c)) {
					return t.getTargetState();
				}
			}
		}
		return null;
	}
	
}
